//
//  EditAddressViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/25.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "EditAddressViewController.h"
#import "ManageViewController.h"
#import "DizhiChoseView.h"

@interface EditAddressViewController ()<UITextViewDelegate,DiZhiChoseDelegate,UITextFieldDelegate>

@end

@implementation EditAddressViewController
{
    UILabel * placeLable;
    
    UITextField * _call,* _name;
    UIButton * _website; //地址chosebtn
    UITextView* _family;
    UITextField *_dangQianTextField;
    
    NSMutableDictionary *_requestDic;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _requestDic = [[NSMutableDictionary alloc]init];
    [self drawNavWithTitle:@"编辑地址" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    self.view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    //保存
    UIButton * guiButton = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-60, 20, 60, 44)];
    [guiButton setTitle:@"保存" forState:UIControlStateNormal];
    [guiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    guiButton.titleLabel.font=[UIFont systemFontOfSize:15];
    [guiButton addTarget:self action:@selector(baoButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guiButton];
    [self take];
}

//设置界面
-(void)take{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 66, KScreenWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UILabel * person =[[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 30)];
    person.font = [UIFont systemFontOfSize:14];
    person.text=@"收货人";
    _name = [[UITextField alloc]initWithFrame:CGRectMake(person.right+10, 10, 100, 30)];
    _name.text = _dizhiDic.consignee;
    _name.font = [UIFont systemFontOfSize:14];
    _name.delegate = self;
    [view addSubview:person];
    [view addSubview:_name];
    
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom+2, KScreenWidth, 50)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    UILabel * telephone =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
    telephone .font = [UIFont systemFontOfSize:14];
    telephone.text=@"联系电话";
    _call = [[UITextField alloc]initWithFrame:CGRectMake(telephone.right+10, 10, 100, 30)];
    _call.text = _dizhiDic.mobile;
    _call.font = [UIFont systemFontOfSize:14];
    _call.delegate = self;
    UIButton * callImage = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-30, 15, 20,20)];
    [callImage setImage:[UIImage imageNamed:@"+"] forState:UIControlStateNormal];
    [callImage addTarget:self action:@selector(call) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:telephone];
    [view2 addSubview:_call];
    [view2 addSubview:callImage];
    
    UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(0, view2.bottom+2, KScreenWidth, 50)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
    address.font = [UIFont systemFontOfSize:14];
    address.text=@"所在地区";
    _website = [[UIButton alloc]initWithFrame:CGRectMake(address.right+10, 10, KScreenWidth-address.right-40, 30)];
    
    [_website setTitle:[NSString stringWithFormat:@"%@/%@/%@",_dizhiDic.province,_dizhiDic.city,_dizhiDic.district] forState:UIControlStateNormal];
    
    [_website setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _website.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    _website.titleLabel.font = [UIFont systemFontOfSize:14];
    [_website addTarget:self action:@selector(placeClicked) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(_website.right+10, 18, 14, 14)];
    image.image = [UIImage imageNamed:@"you"];
    [view3 addSubview:address];
    [view3 addSubview:image];
    [view3 addSubview:_website];
    
    _family = [[UITextView alloc]initWithFrame:CGRectMake(0, view3.bottom+2, KScreenWidth, 90)];
    _family.delegate = self;
    _family.text = _dizhiDic.address;
    [self.view addSubview:_family];
    placeLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
    placeLable.textColor = [UIColor lightGrayColor];
    placeLable.font = [UIFont systemFontOfSize:13];
    placeLable.backgroundColor = [UIColor clearColor];
    [_family addSubview:placeLable];
    
    UIView * lastView = [[UIView alloc]initWithFrame:CGRectMake(0, _family.bottom+10, KScreenWidth, 50)];
    lastView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lastView];
    UIButton * removeButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 15, 100, 20)];
    removeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [removeButton setTitle:@"删除地址" forState:UIControlStateNormal];
    [removeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [removeButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    [lastView addSubview:removeButton];
    
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        placeLable.text = @"请填写详细地址,不少于5个字";
    }else{
        placeLable.text = @"";
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    _dizhiDic.address = textView.text;
}

//保存按钮
-(void)baoButton{
    [_family resignFirstResponder];
    [_dangQianTextField resignFirstResponder];
    if ([_dizhiDic.consignee length] == 0) {
        [[iToast makeText:@"请输入收货人姓名"]show];
        return;
    }
    if ([_dizhiDic.mobile length] == 0) {
        [[iToast makeText:@"请输入收货人电话"]show];
        return;
    }
    if ([_dizhiDic.address length] < 5) {
        [[iToast makeText:@"请输入收货人详细地址"]show];
        return;
    }
    [_requestDic setObject:_dizhiDic.consignee forKey:@"consignee"];
    [_requestDic setObject:_dizhiDic.mobile forKey:@"mobile"];
    [_requestDic setObject:_dizhiDic.address forKey:@"address"];
    [_requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [_requestDic setObject:_dizhiDic.addressId forKey:@"addressId"];
    [NetWorking NEWjieKouQingQiu:DZ_xiuGaiDiZhi ANDIP:IP And:_requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        if(!error){
            
            if ([[dic objectForKey:Code]integerValue] == 200) {
                //修改地址成功
                [[NSNotificationCenter defaultCenter]postNotificationName:@"diZhitableShuaXin" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
    
}
//删除地址
-(void)remove{
    
    NSMutableDictionary *deleteDic = [[NSMutableDictionary alloc]init];
    [deleteDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [deleteDic setObject:_dizhiDic.addressId forKey:@"addressId"];
    
    [NetWorking NEWjieKouQingQiu:DZ_shanChuDiZhi ANDIP:IP And:deleteDic anBlock:^(NSDictionary *dic, NSError *error) {
        
        if (!error) {
            if ([[dic objectForKey:Code]integerValue] == 200) {
                //删除成功
                [[NSNotificationCenter defaultCenter]postNotificationName:@"diZhitableShuaXin" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    _dangQianTextField = textField;
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == _name) {
        _dizhiDic.consignee = _name.text;
        
    }else if(textField == _call){
        _dizhiDic.mobile = textField.text;
    }
}
//进入通讯录
-(void)call{
    
}
//选择地址
-(void)placeClicked{
    [_dangQianTextField resignFirstResponder];
    [_family resignFirstResponder];
    DizhiChoseView * diZhiView = [[DizhiChoseView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    diZhiView.delegate = self;
    [self.view addSubview:diZhiView];
}
-(void)choseEndClicked:(NSArray*)ary{
    NSString *diZhiStr = [NSString stringWithFormat:@"%@/%@/%@",[[ary objectAtIndex:0]objectForKey:@"regionName"],[[ary objectAtIndex:1]objectForKey:@"regionName"],[[ary objectAtIndex:2]objectForKey:@"regionName"]];
    [_website setTitle:diZhiStr forState:UIControlStateNormal];
    
    [_requestDic setObject:[[ary objectAtIndex:0]objectForKey:@"regionId"] forKey:@"province"];
    [_requestDic setObject:[[ary objectAtIndex:1]objectForKey:@"regionId"] forKey:@"city"];
    [_requestDic setObject:[[ary objectAtIndex:2]objectForKey:@"regionId"] forKey:@"district"];
//    [_dizhiDic setObject:[[ary objectAtIndex:0]objectForKey:@"name"] forKey:@""];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
